package com.dbg.ui.animation;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class RotationImage extends JPanel implements ActionListener {

    private BufferedImage image;
    private Timer timer;
    private double angle = 0;
    private boolean rotating = true;

    public RotationImage() {
        // 读取 resources 下的图片
        try {
            image = ImageIO.read(getClass().getClassLoader().getResourceAsStream("image/loading.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 图片缩放到150x150
        Image scaledImage = image.getScaledInstance(150, 150, Image.SCALE_SMOOTH);
        BufferedImage bufferedScaledImage = new BufferedImage(150, 150, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = bufferedScaledImage.createGraphics();
        g2d.drawImage(scaledImage, 0, 0, null);
        g2d.dispose();
        image = bufferedScaledImage;

        // 定时器控制旋转
        timer = new Timer(20, this);
        timer.start();
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        int x = (getWidth() - image.getWidth()) / 2;
        int y = 10; // 图片距离顶部的距离

        // 绘制旋转的图片
        AffineTransform at = AffineTransform.getRotateInstance(Math.toRadians(angle), image.getWidth() / 2, image.getHeight() / 2);
        AffineTransformOp rotateOp = new AffineTransformOp(at, AffineTransformOp.TYPE_BILINEAR);
        g2d.drawImage(rotateOp.filter(image, null), x, y, null);

        // 在图片下方显示“加载中”
        g2d.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        g2d.setColor(Color.BLACK);
        String loadingText = "加载中...";
        FontMetrics fm = g2d.getFontMetrics();
        int textX = (getWidth() - fm.stringWidth(loadingText)) / 2;
        int textY = y + image.getHeight() + fm.getHeight();
        g2d.drawString(loadingText, textX, textY);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(150, 210); // 窗体大小150x210
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (rotating) {
            angle += 2;
            repaint();
        }
    }

    // 停止旋转的函数
    public void stopRotation() {
        rotating = false;
    }

    // 主程序
    public static void main(String[] args) {
        // 使用JWindow创建无边框窗体，并设置透明
        JWindow window = new JWindow();
        RotationImage rotationPanel = new RotationImage();
        rotationPanel.setBackground(new Color(255, 255, 255, 0));
        window.add(rotationPanel);
        window.setSize(150, 210);
        window.setLocationRelativeTo(null); // 窗体居中

        // 设置背景为透明
        window.setBackground(new Color(0, 0, 0, 0));
        window.setVisible(true);
    }
}
